package com.lxw.manage_cms.controller;

import com.lxw.api.cms.CmsTemplateControllerApi;
import com.lxw.framework.domain.cms.CmsPage;
import com.lxw.framework.domain.cms.CmsTemplate;
import com.lxw.framework.model.response.CommonCode;
import com.lxw.framework.model.response.QueryResponseResult;
import com.lxw.framework.model.response.QueryResult;
import com.lxw.framework.model.response.ResponseResult;
import com.lxw.manage_cms.service.ICmsGridFsService;
import com.lxw.manage_cms.service.ICmsPageService;
import com.lxw.manage_cms.service.ICmsTemplateService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;


@RestController
@RequestMapping("template")
public class CmsTemplateController implements CmsTemplateControllerApi {


    @Autowired
    ICmsTemplateService cmsTemplateService;
    @Autowired
    ICmsGridFsService cmsGridFsService;

    @GetMapping("/listAll")
    @Override
    public QueryResponseResult findTemplateAll() {
        //查找所有的模板信息
        List<CmsTemplate> templates = cmsTemplateService.findTemplateAll();
        QueryResult<CmsTemplate> cmsTemplateQueryResult = new QueryResult<>();
        cmsTemplateQueryResult.setList(templates);
        cmsTemplateQueryResult.setTotal(templates.size());

        QueryResponseResult result = new QueryResponseResult(CommonCode.SUCCESS,cmsTemplateQueryResult);

        return result;
    }

    @GetMapping("/list/{siteId}/{tempName}")
    @Override
    public QueryResponseResult findTemplate(@PathVariable String siteId
            , @PathVariable String tempName) {

        List<CmsTemplate> templates = cmsTemplateService.findTemplate(siteId,tempName);
        QueryResult<CmsTemplate> cmsTemplateQueryResult = new QueryResult<>();
        cmsTemplateQueryResult.setList(templates);
        cmsTemplateQueryResult.setTotal(templates.size());

        QueryResponseResult result = new QueryResponseResult(CommonCode.SUCCESS,cmsTemplateQueryResult);
        return result;
    }


    @PostMapping("/add")
    @Override
    public ResponseResult addTemplate(@RequestBody CmsTemplate cmsTemplate){
        //非空校验
        if (cmsTemplate.getTemplateName() == null || "".equals(cmsTemplate.getTemplateName())){
            throw new RuntimeException("参数为空");
        }
        if (cmsTemplate.getSiteId() == null || "".equals(cmsTemplate.getSiteId())){
            throw new RuntimeException("参数为空");
        }
        if (cmsTemplate.getTemplateFileId() == null || "".equals(cmsTemplate.getTemplateFileId())){
            throw new RuntimeException("参数为空");
        }


        cmsTemplateService.addTemplate(cmsTemplate);
        ResponseResult result = new ResponseResult();
        result.setCode(2000);
        result.setMessage("添加成功");
        result.setSuccess(true);

        return result;
    }

    @PostMapping("/store")
    @Override
    public ObjectId storeTemplate(@RequestParam String url, @RequestParam String fileName) throws FileNotFoundException {

        FileInputStream fileInputStream = new FileInputStream(new File(url));
        ObjectId id = cmsGridFsService.addTemplate(fileInputStream, fileName);
        return id;
    }
}
